<template>
	<view>
		<component
		  ref="freeCom"
		  v-for="(item, index) in list"
		  :key="item.id"
		  :is="item.type"
		  :item="setItem(item)"
		  :number="index"
		  :uploadUrl="uploadUrl"
		  :uploadHeader="uploadHeader"
		  @changeComponent="getBackVal"
		></component>
	</view>
</template>

<script>
import * as FormItem from "../uni-form.js";
export default {
  props: {
    requestData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    uploadUrl: {
      type: String,
      default: () => {
        return "";
      },
    },
    uploadHeader: {
      type: Object,
      default: () => {
        return "";
      },
    },
	defaultProps: {
	  type: Object,
	  default: () => {
	    return "";
	  },
	},
	defaultComponentsName: {
		type: Object,
		default: () => {
		  return "";
		},
	}
  },
  components: FormItem,
  computed:{
	setItem() {
		return function (item) {
			let obj = {
				// ...item,
				must: item[this.defaultProps.must],
				selectData: item[this.defaultProps.selectData],
				title: item[this.defaultProps.title],
				msg: item[this.defaultProps.msg],
				type: item[this.defaultProps.type]
			}
		}
	},
  },
  data() {
    return {
      list: [],
    };
  },
  methods: {
    // 获取控件输入返回值
    getBackVal(obj) {
      this.list[obj.number].value = obj.value;
      this.$emit("getVal", {
        list: this.list,
      });
    },
    // 表单提交
    submit(cb) {
      let results = this.list.map((val, index) => {
        return new Promise((resolve, reject) => {
			this.$children[0].$children[index].$refs["uform"].validate((valid) => {
			  if (valid) {
			    resolve();
			  } else {
			    reject();
			  }
			});
        });
      });
      return Promise.all(results)
        .then((res) => {
          cb(true);
        })
        .catch((err) => {
          cb(false);
        });
    },
  },
  mounted() {
    this.list = this.requestData;
  },
};
</script>

<style>
</style>
